import adminAuths from '@/mock/adminAuths'
import teacherAuths from '@/mock/teacherAuths'
import studentAuths from '@/mock/studentAuths'

function showError(result) {
  ElMessage({
    type: 'error',
    message: result.message || 'error',
    dangerouslyUseHTMLString: true,
  })
  console.log(result.message || 'error')
}

// 响应时间
const responseTimes = 1000

export default {
  // 登录接口
  login(username, password) {
    return new Promise((resolve, reject) => {
      setTimeout(function () {
        let result = {
          code: 200,
          message: '请求成功',
          data: {}
        }
        if (username === 'admin' && password === '123456') {
          result.data = {
            id: 1,
            nickname: '管理员',
            roles: ['admin'],
            token: 'token-admin',
            auths: adminAuths,
          }
          resolve(result)
        } else if (username === 'teacher' && password === '123456') {
          result.data = {
            id: 2,
            nickname: '教师',
            roles: ['teacher'],
            token: 'token-teacher',
            auths: teacherAuths
          }
          resolve(result)
        } else if (username === 'student' && password === '123456') {
          result.data = {
            id: 3,
            nickname: '学生',
            roles: ['student'],
            token: 'token-student',
            auths: studentAuths
          }
          resolve(result)
        } else {
          let errResult = {
            code: 201,
            message: `
              <div>账号或密码有误！</div>
              <div>按F12，看控制台提示！！！</div>
              <div>管理员账号：admin，密码：123456</div>
              <div>教师账号：teacher，密码：123456</div>
              <div>学生账号：student，密码：123456</div>
            `,
            data: {}
          }
          showError(errResult)
          reject(errResult)
        }
      }, responseTimes)
    })
  },
  // 获取权限
  getAuths(token) {
    return new Promise((resolve, reject) => {
      setTimeout(function () {
        let result = {
          code: 200,
          message: '请求成功',
          data: {}
        }
        if (token === 'token-admin') {
          result.data = {
            roles: ['admin'],
            auths: adminAuths,
          }
          resolve(result)
        } else if (token === 'token-teacher') {
          result.data = {
            roles: ['teacher'],
            auths: teacherAuths
          }
          resolve(result)
        } else if (token === 'token-student') {
          result.data = {
            roles: ['student'],
            auths: studentAuths
          }
          resolve(result)
        } else {
          let errResult = {
            code: 201,
            message: `
              <div>token有误！</div>
            `,
            data: {}
          }
          showError(errResult)
          reject(errResult)
        }
      }, responseTimes)
    })
  }
}